﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RenameTools
{
    public partial class Form1 : Form
    {
        private string RootPath { get; set; }

        private readonly string Temp = "T" + "emplate";
        public Form1()
        {
            InitializeComponent();
            GetSlnPath();
        }


        private void button1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.textBox1.Text))
            {
                MessageBox.Show($"请输入要替换的服务名称");
                return;
            }
            else if(this.label2.Text !=$"【{Temp}】")
            {
                MessageBox.Show($"该程序已经替换！");
                return;
            }
            List<string> list = new List<string>();
            GetDirectories(RootPath, ref list);
            MoveDirectories(list);

        }

        private void MoveDirectories(List<string> list)
        {
            var newls = new List<string>();
            foreach (var item in list)
            {
                FileInfo info = new FileInfo(item);
                var newdir = item.Replace(Temp, this.textBox1.Text);
                ReplaceInfo(item, newdir);
            }
        }

        private void ReplaceInfo(string olddir, string newdir)
        {
            FileInfo info = new FileInfo(newdir);
            DirectoryInfo d = info.Directory;
            if (!d.Exists) {
                d.Create();
            };
            File.Move(olddir, newdir);

            var textStreamReader = new StreamReader(newdir);
            string content = textStreamReader.ReadToEnd();
            textStreamReader.Close();
            var newContent= content.Replace(Temp, this.textBox1.Text);

            File.WriteAllText(newdir, newContent, Encoding.UTF8);
        }

        readonly List<string> IgnoreDirec = new List<string>() { 
        ".git",
        ".vs",
        "bin",
        "obj"
        };
        

        public void GetDirectories(string dirPath, ref List<string> list)
        {
           
            DirectoryInfo directoryInfo = new DirectoryInfo(dirPath);
            foreach (FileInfo info in directoryInfo.GetFiles("*"))
            {
                Console.WriteLine(info.FullName);
                list.Add(info.FullName);
            }
            foreach (DirectoryInfo info in directoryInfo.GetDirectories())
            {
                if (IgnoreDirec.Contains(info.Name))
                {
                    continue;
                }
                GetDirectories(info.FullName, ref list);
            }

        }

        private void GetSlnPath()
        {
            var path = Path.GetFullPath("..\\..\\..");
            RootPath = path;
            var sln = Directory.GetFiles(path, "*.sln")?.FirstOrDefault();

            this.label2.Text = $"【{sln.Split('.')[1]}】";
        }
    }
}
